﻿using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Epitech.Krisk.Xna.UI.Game.InGame;
using Epitech.Krisk.Xna.ScreenManagerLib.ScreenManager;

namespace Epitech.Krisk.Xna.UI.Game.Utils
{
    public class RenderBackground : AGameObject
    {
        String _bg;
        Texture2D _background;
        protected Rectangle _areabg;
        protected Rectangle _dest;

        public RenderBackground(string pathbg)
        {
            _bg = pathbg;
        }

        public override void LoadContent()
        {
            _background = Content.Load<Texture2D>(_bg);
        }

        Viewport _lastsize;

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            Viewport resolution = ViewPort;
            double ratio = (double)resolution.Width / (double)resolution.Height;

            if (_background.Width >= _background.Height)
                _areabg = new Rectangle((_background.Width / 2 - (int)(_background.Height * ratio) / 2), 0,
                                        (int)(_background.Height * ratio), _background.Height);
            else if (_background.Width < _background.Height)
                _areabg = new Rectangle(0, (_background.Height / 2 - (int)(_background.Width / ratio) / 2),
                                        _background.Width, (int)(_background.Width / ratio));
            _dest = new Rectangle(0, 0, resolution.Width, resolution.Height);
            _lastsize = ViewPort;
        }

        public override void UnloadContent()
        {
        }

        public override void HandleInput(InputState input)
        {
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sp = SpriteBatch;

            sp.Begin();
            sp.Draw(_background, _dest, _areabg, Color.White);
            sp.End();
        }
    }
}
